home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #4 / Amiga Plus CD - 2000 - No. 4.iso / Tools / Dev / SpeakFreely_Src / lpc10 / onset.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-27  |  2.2 KB  |  95 lines

  1. /*******************************************************************
  2. *
  3. *    ONSET Version 49
  4. *
  5. *******************************************************************
  6. *
  7. *    Floating point version
  8. */
  9.  
  10. #include "config.ch"
  11. #include "lpcdefs.h"
  12. #include <math.h>
  13.  
  14. onset( pebuf, osbuf, osptr)
  15. float pebuf[];
  16. int osbuf[], *osptr;
  17. {
  18. /*   Detection of onsets in (or slightly preceding) the futuremost frame
  19. *   of speech.
  20.  
  21. *   Arguments
  22. *    PEBUF    Preemphasized speech
  23. *    OSBUF    Buffer which holds sorted indexes of onsets (Modified)
  24. *    OSPTR    Free pointer into OSBUF (Modified)
  25.  
  26. *   Parameters for onset detection algorithm:
  27. *    L2        Threshold for filtered slope of FPC (function of L2WID!)
  28. *    L2LAG    Lag due to both filters which compute filtered slope of FPC
  29. *    L2WID    Width of the filter which computes the slope of FPC
  30. *    OSHYST    The number of samples which of slope(FPC) which must be below
  31. *            the threshold before a new onset may be declared.
  32. *   Variables
  33. *    N, D       Numerator and denominator of prediction filters
  34. *    FPC        Current prediction coefs
  35. *    L2BUF, L2SUM1, L2SUM2    State of slope filter
  36. */
  37.  
  38. int l2lag=9, l2wid=16, oshyst=10;
  39. float l2=1.7;
  40.  
  41. static float n=0., d=1., fpc;
  42. static float l2buf[16], l2sum1=0., l2sum2=0.;
  43. static int l2ptr1=1, l2ptr2=9, lasti;
  44. int i;
  45. static short hyst=0;
  46. static short first=1;
  47.  
  48.  
  49. if (hyst) lasti -= 180;
  50.  
  51.  
  52. for(i=SBUFH-LFRAME+1; i<=SBUFH;i++)  {
  53.  
  54. /*   Compute FPC; Use old FPC on divide by zero; Clamp FPC to +/- 1.    */
  55.  
  56.    n=(pebuf[i]*pebuf[i-1]+63.*n) * 0.015625;
  57.    d=(pebuf[i-1]*pebuf[i-1]+63.*d) * 0.015625;
  58.  
  59.    if (d != 0.) {
  60.       if ((float)fabs((double)n) > d) {
  61.          /*fpc = sign(1., n);*/
  62.     fpc = (n<0)?-1.:1.;
  63.       }
  64.       else
  65.          fpc=n/d;
  66.    }
  67.  
  68. /*   Filter FPC    */
  69.    l2sum2 = l2buf[l2ptr1-1];
  70.    l2sum1 = l2sum1 - l2buf[l2ptr2-1] + fpc;
  71.    l2buf[l2ptr2-1] = l2sum1;
  72.    l2buf[l2ptr1-1] = fpc;
  73.    l2ptr1 = (l2ptr1%l2wid)+1;
  74.    l2ptr2 = (l2ptr2%l2wid)+1;
  75.  
  76.    if ((float)fabs((double)(l2sum1-l2sum2)) > l2) {  
  77.       if (!hyst) { 
  78. /*   Ignore if buffer full    */
  79.          if (*osptr <= OSLEN) { 
  80.             osbuf[*osptr] = i - l2lag;
  81.             *osptr = *osptr + 1;
  82.          } 
  83.          hyst = 1;
  84.       } 
  85.       lasti = i;
  86.    }
  87.    else  if (hyst && i - lasti >= oshyst) { 
  88.       hyst = 0;
  89.    } 
  90.  
  91. } /* end while */
  92.  
  93.  
  94. }
  95.